<?php
/**
 * User namagement page controller
 *
 * @author Dima
 *
 */

class Admin_ProjectController extends modules_admin_controllers_ControllerBase
{
    protected $_bForLoggedUsersOnly = true;
    
//----------------------------------------------------------------------------------------------------
    
    public function init()
    {
        if (! $this->getRequest()->isXmlHttpRequest())
        {
            parent::init();
            $this->view->title = 'Project Management';
            
            $this->view->headLink()->appendStylesheet('/css/user.css');
            $this->view->headLink()->appendStylesheet('/css/ui-lightness/jquery-ui-1.7.2.custom.css');
            
            $this->view->headScript()->appendFile('/js/usertoproject.js');
            $this->view->headScript()->appendFile('/js/lib/jquery-ui.js');
        }
        
        $this->_helper->AjaxContext()->initContext('json');     
    }
    
//----------------------------------------------------------------------------------------------------

    public function indexAction()
    {
        $this->view->headLink()->appendStylesheet('/css/default.css');
        $this->view->headScript()->appendFile('/js/jquery.tablesorter.js');
        $this->view->headScript()->appendFile('/js/sort.js');               
        
        $oRequest    = $this->getRequest();
        $page        = $oRequest->getParam('page');
        $aItems      = models_ProjectMapper::getAll($page, 99999999, true);
        $allInReport = models_UsertoprojectMapper::getAllDistinct();
        
        $this->view->allInReport = $allInReport;
        $this->view->paginator = $aItems['paginator'];        
    }
    
//----------------------------------------------------------------------------------------------------

    public function editAction()
    {
        $oRequest = $this->getRequest();
        $oForm = new modules_default_forms_ProjectForm();

        if ($oRequest->isPost())
        {
            $aPost = $oRequest->getPost();

            $oForm->isValid($aPost);

            $aData = array(
                    'name'          =>  $oForm->getValue('name'),
                    'base'          =>  'on' == $oForm->getValue('base') ? 1 : 0 , 
                    'start_date'    =>  $oForm->getValue('start_date'),
                    'end_date'      =>  $oForm->getValue('end_date'),
                    'project_num'   =>  $oForm->getValue('project_num'),
                    'time_off'      =>  'on' == $oForm->getValue('time_off') ? 1 : 0 , 
                    'billable'      =>  'on' == $oForm->getValue('billable') ? 1 : 0 ,
            );

            models_ProjectMapper::edit($this->_request->getParam('id'), $aData);
            
            $this->_redirect('/admin/project/');
        }
        else
        {
            $this->view->oItem = models_ProjectMapper::findById($this->_request->getParam('id'));
        }
    }
    
//----------------------------------------------------------------------------------------------------

    public function addAction()
    {
        $oRequest           = $this->getRequest();
        
        if ($oRequest->isPost())
        {
            $name                   = strip_tags($oRequest->getParam('name'));
            $base                   = strip_tags($oRequest->getParam('base'));
            $project_num            = strip_tags($oRequest->getParam('project_num'));
            $start_date             = strip_tags($oRequest->getParam('start_date'));
            $end_date               = strip_tags($oRequest->getParam('end_date'));
            $time_off               = strip_tags($oRequest->getParam('time_off'));

            $aData['name']          = $name;
            $aData['project_num']   = $project_num;
            $aData['start_date']    = $start_date;
            $aData['end_date']      = $end_date;
            $aData['base']          = 'on' == $base ? 1 : 0 ;
            $aData['time_off']      = 'on' == $time_off ? 1 : 0 ;
            
            $exist_project          = models_ProjectMapper::findByName($name);
            if (isset($exist_project->id))
            {
            	$id = $exist_project->id;
            	$aData = array('in_list' => 1);
                models_ProjectMapper::edit($id, $aData);
            }
            else
            {
                $idItem                 = models_ProjectMapper::save($aData);
            }
            $this->_redirect('/admin/project');
        }
        else
        {
        }
    }
    
//----------------------------------------------------------------------------------------------------

    public function deleteAction()
    {
        try 
        {
//            models_ProjectMapper::delete($this->_request->getParam('id'));
            $aData = array('in_list' => 0);
            models_ProjectMapper::edit($this->_request->getParam('id'), $aData);
        } 
        catch (Exception $e) 
        {
        }
            $this->_redirect('/admin/project/');
    }
    
//----------------------------------------------------------------------------------------------------
    
}